package com.liming.user.controller;

import com.liming.common.pojo.dto.order.user.UserOrderDTO;
import com.liming.common.pojo.dto.order.user.mini.MiniOrderEvaluateDTO;
import com.liming.common.pojo.dto.order.user.mini.MiniOrderQueryPage;
import com.liming.common.pojo.dto.order.user.mini.MiniOrderSelectCouponDTO;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.coupon.mini.MiniOrderCouponVO;
import com.liming.common.pojo.vo.order.mini.MiniOrderDetailVO;
import com.liming.common.pojo.vo.order.mini.MiniOrderSelectCouponVO;
import com.liming.common.pojo.vo.order.mini.MiniOrderVO;
import com.liming.common.pojo.vo.order.web.RefundOrderVO;
import com.liming.common.result.LMResult;
import com.liming.order.client.UserOrderClient;
import com.liming.service.wx.pojo.vo.PrepayVO;
import com.liming.user.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author liming
 * @date 2025/3/7
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "订单接口-Web")
@RequestMapping("/user/order")
public class OrderController {

    private final UserOrderClient orderClient;
    private final OrderService orderService;

    @Operation(
            summary = "分页 - 用户订单",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniOrderVO.class))
                    })
            }
    )
    @GetMapping
    public LMResult<PageVO<MiniOrderVO>> page(MiniOrderQueryPage queryPage) {
        return LMResult.success(orderService.page(queryPage));
    }

    @Operation(
            summary = "查询用户订单 能够使用的优惠券、不能使用的优惠券",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniOrderCouponVO.class))
                    })
            }
    )
    @GetMapping("coupon_list")
    public LMResult<MiniOrderCouponVO> couponList(Integer amount) {
        return LMResult.success(orderService.orderCoupon(amount));
    }

    @Operation(
            summary = "订单详情",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniOrderDetailVO.class))
                    })
            }
    )
    @GetMapping("detail/{id}")
    public LMResult<MiniOrderDetailVO> detail(@PathVariable Long id) {
        return LMResult.success(orderService.detail(id));
    }

    @Operation(summary = "退款详情 - 用户")
    @GetMapping("refundDetail/{id}")
    public LMResult<RefundOrderVO> refundDetail(@PathVariable Long id) {
        return LMResult.success(orderService.refundDetail(id));
    }

    @Operation(
            summary = "用户在订单中 选择优惠券",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = "application/json", schema = @Schema(implementation = MiniOrderSelectCouponVO.class))
                    })
            }
    )
    @PostMapping("select_coupon")
    public LMResult<MiniOrderSelectCouponVO> selectCoupon(@RequestBody MiniOrderSelectCouponDTO dto) {
        return LMResult.success(orderService.selectCoupon(dto));
    }

    @Operation(summary = "用户下单")
    @PostMapping("createOrder")
    public LMResult<PrepayVO> createOrder(@RequestBody UserOrderDTO userOrderDTO) {
        PrepayVO result = orderClient.createOrder(userOrderDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "订单评价")
    @PostMapping("evaluate")
    public LMResult<Boolean> evaluate(@RequestBody MiniOrderEvaluateDTO miniOrderEvaluateDTO) {
        return LMResult.success(orderService.evaluate(miniOrderEvaluateDTO));
    }

    @Operation(summary = "退款申请 -- 订单")
    @GetMapping("refund/{id}")
    public LMResult<Boolean> refund(@PathVariable Long id, @RequestParam String reason) {
        return LMResult.success(orderService.refund(id, reason));
    }

    @Operation(summary = "支付成功 调用接口")
    @PutMapping("createOrderPaySuccess/{id}")
    public LMResult<Boolean> createOrderPaySuccess(@PathVariable Long id) {
        return LMResult.success(orderService.createOrderPaySuccess(id));
    }

    @Operation(summary = "取消订单")
    @PutMapping("cancel/{id}")
    public LMResult<Boolean> cancel(@PathVariable Long id) {
        return LMResult.success(orderService.cancel(id));
    }

    @Operation(summary = "完成订单")
    @PutMapping("complete/{id}")
    public LMResult<Boolean> complete(@PathVariable Long id) {
        return LMResult.success(orderService.complete(id));
    }
}
